// pages/setCurrent/setCurrent.js
var app=getApp()
var selected_devNo=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    minelec:0,
    maxelec:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    selected_devNo = JSON.parse(options.selected_devNo);
  },

  formSubmit: function (e) {
    let that = this;
    // console.log(e, "e");
    that.setData({
      minelec: e.detail.value.input_minelec,
      maxelec: e.detail.value.input_maxelec
    });

    let header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }

    if (that.data.maxelec == "") {
      wx.showModal({
        title: '提示',
        content: '最大电流值不能为空',
        showCancel: false
      });
      return;
    }

    if (that.data.minelec == "") {
      wx.showModal({
        title: '提示',
        content: '最小电流值不能为空',
        showCancel: false
      });

      return;
    }

    if (parseInt(that.data.minelec) >= parseInt(that.data.maxelec)){
      wx.showModal({
        title: '提示',
        content: '最小电流值不能大于最大电流值',
        showCancel: false
      });
    }

    that.saveCurrentSetting();
  },

  formReset: function (e) {
    let that = this;

    let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
    let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值 
    wx.navigateBack(); //关闭当前页面，返回上一个页面    
  },

  saveCurrentSetting:function()
  {   
    let that = this;
    let devNo = selected_devNo.join(',') + "";
    let header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    } 

    wx.request({
      url: "https://119.3.46.145/server/UpdateSingleLightCurrent",
      method:'post',
      header:header,
      data: {
        devNo: devNo,
        maxCurrent: that.data.maxelec,
        minCurrent: that.data.minelec,
        userid: app.globalData.userid
      },
      success: function (response) {
        let obj = response.data;
        if (obj.error === "0") {
          // $("#currentSetDialog").window("close");
          // initSingleLightTable();
          // alert("修改成功");
          wx.showModal({
            title: '提示',
            content: '批量修改电流成功',
            showCancel: false,
            success: res => {
              wx.navigateBack();
            }
          });
        }
        else{
          wx.showModal({
            title: '提示',
            content: '批量修改电流失败',
            showCancel: false           
          });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})